package com.itheima.im.socket.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import com.itheima.im.socket.R;
import com.itheima.im.socket.bean.QQMessage;
import com.itheima.im.socket.bean.QQMessageType;
import com.itheima.im.socket.core.OnReceiveMsgListener;
import com.itheima.im.socket.core.QQConnnection;
import com.itheima.im.socket.service.ChatService;
import com.itheima.im.socket.utils.ThreadUtils;

public class LoginActivity extends Activity {
	@InjectView(R.id.username)
	EditText username;
	@InjectView(R.id.pwd)
	EditText pwd;
	@InjectView(R.id.login)
	Button login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		// 初始化注解生效
		ButterKnife.inject(this);
		System.out.println(login);
		ThreadUtils.runInThread(new Runnable() {
			@Override
			public void run() {
				if (conn == null) {
					try {
						// 192.168.15.97 5225
						conn = new QQConnnection("192.168.0.102", 5225);
						conn.addOnReceiveMsgListener(listener);
						conn.connect();// start等待线程
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		conn.removeOnReceiveMsgListener(listener);
	}

	private OnReceiveMsgListener listener = new OnReceiveMsgListener() {
		@Override
		public void onReceive(final QQMessage msg) {
			// 子线程
			ThreadUtils.runUIThread(new Runnable() {
				@Override
				public void run() {
					if (msg.type.equals(QQMessageType.MSG_TYPE_BUDDYLIST)) {
						System.out.println(msg.content);
						String json = msg.content;
						// 保存消息通道
						MyApp.conn = conn;
						// 保存账号
						MyApp.username = usernameString;
						Toast.makeText(getBaseContext(), "登录成功!", Toast.LENGTH_SHORT).show();

						Intent service = new Intent(getBaseContext(), ChatService.class);
						startService(service);

						Intent intent = new Intent(getBaseContext(), QQContactActivity.class);

						intent.putExtra("json", json);
						startActivity(intent);
						finish();
					} else {
						Toast.makeText(getBaseContext(), "账号或者密码出错!", Toast.LENGTH_SHORT).show();
					}

				}
			});

		}
	};
	private QQConnnection conn = null;
	String usernameString;
	String pwdString;

	@OnClick(R.id.login)
	public void login(View view) {

		usernameString = username.getText().toString().trim();
		pwdString = pwd.getText().toString().trim();
		// 192.168.15.97 5225
		// Toast.makeText(this, "登录", 0).show();
		ThreadUtils.runInThread(new Runnable() {
			@Override
			public void run() {
				try {
					// ①　创建消息通道
					// ②　发送消息
					QQMessage msg = new QQMessage();
					msg.type = QQMessageType.MSG_TYPE_LOGIN;
					// ③　创建监听接收消息
					msg.content = usernameString + "#" + pwdString;
					// ④　提示:成功/失败
					conn.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});

	}
}
